package com.qf.dao.impl;

import com.qf.dao.CartDao;
import com.qf.entity.Cart;
import com.qf.entity.Goods;
import com.qf.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 10202
 * @version V1.0
 * @Project xiaomi
 * @Package com.qf.dao.impl
 * @Date 2023/4/14 15:56
 */
public class CartDaoImpl implements CartDao {
    @Override
    public Cart query(Integer uid, int gid) {
        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());

        try {
            return qr.query("select * from tb_cart where id=? and pid=?",new BeanHandler<>(Cart.class),uid,gid);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }

    @Override
    public void createCart(Cart newCart) {
        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());

        Object[] params={newCart.getId(),newCart.getPid(),newCart.getNum(),newCart.getMoney()};
        try {
            qr.update("insert into tb_cart values (?,?,?,?)",params);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }

    @Override
    public void updateCart(Cart cart) {
        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());

        Object[] params={cart.getNum(),cart.getMoney(),cart.getId(),cart.getPid()};
        try {
            qr.update("update tb_cart set num=? ,money=? where id=? and pid=? ",params);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }


    }

    @Override
    public List<Cart> queryByUid(Integer uid) {
        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());


        try {
            return qr.query("select g.*,c.num,c.money from tb_cart as c inner join tb_goods as g on c.pid=g.id where c.id=?", new ResultSetHandler<List<Cart>>() {
                @Override
                public List<Cart> handle(ResultSet resultSet) throws SQLException {
                    List<Cart> cartList=new ArrayList<>();
                    while (resultSet.next()){
                        int num = resultSet.getInt("num");
                        BigDecimal money = resultSet.getBigDecimal("money");
                        int gid = resultSet.getInt("id");
                        String name = resultSet.getString("name");
                        Date pubdate = resultSet.getTimestamp("pubdate");
                        String picture = resultSet.getString("picture");
                        BigDecimal price = resultSet.getBigDecimal("price");
                        int star = resultSet.getInt("star");
                        String intro = resultSet.getString("intro");
                        int typeId = resultSet.getInt("typeid");
                        Cart cart=new Cart(uid,gid,num,money,new Goods(gid,name,pubdate,picture,price,star,intro,typeId,null));
                        cartList.add(cart);
                    }
                    return cartList;
                }
            },uid);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }

    @Override
    public void deleteCart(Integer uid, int gid) {
        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());
        try {
            qr.update("delete from tb_cart where id=? and pid=?",uid,gid);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }

    }

    @Override
    public void deleteCart(Integer uid) {
        QueryRunner qr=new QueryRunner();
        Connection conn = DruidUtils.getConnection();
        try {
            qr.update(conn,"delete from tb_cart where id=? ",uid);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }finally {
            DruidUtils.closeAll(null,null,conn);
        }

    }
}
